package com.mingyizhudao.app.http;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

import com.baidu.mobstat.StatService;
import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.http.DataHandler;
import com.dixiang.framework.network.IResult;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.common.IDataLoadListenter;
import com.mingyizhudao.app.common.IDataLoadListenterByFlg;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.utils.ToastUtil;
import com.mingyizhudao.app.view.doctor.bean.DoctorsEntity;
import com.mingyizhudao.app.view.mine.bean.OrderItemEntity;
import com.mingyizhudao.app.view.search.bean.SearchEntity;

public class DataUtils {

	private static DataHandler handler;
	private static DataUtils object;

	public DataUtils() {
		handler = new DataHandler();
	}

	public static DataUtils getInitial() {
		if (object == null) {
			object = new DataUtils();
		}
		return object;
	}

	public static void getDataObject(Activity activity, String url,
			String params, TypeToken typeToken,
			QJNetUICallback callback) {
		String path = handler.getDataObject(activity, url, params, typeToken,
				callback);
	}

	/**
	 * 获取本地数据版本编号
	 * 
	 * @param activity
	 * @param map
	 */
	public static void getCheckData(final Context activity,
			String params,final Handler mHandler) {

		QJNetUICallback callback = new QJNetUICallback<QjResult<Map<String,Object>>>(
				activity) {
			@Override
			public void onSuccess(QjResult<Map<String,Object>> result) {
				if (result != null) {
					Map<String, Object> map = result.getResults();
					if(map != null && !map.isEmpty()){
						String localUrl = map.get("localdataUrl") + "";
						CommonUtils.setLocaldataFromNetUrl(activity,localUrl);
						int version = Integer.parseInt(map.get("version") + "");
						int currentDataVersion = Integer.parseInt(CommonUtils.getDataVersion(activity));
						if (version > currentDataVersion) {
							 getLocaldataFromNet(activity , version , mHandler);
						}else{
							mHandler.sendEmptyMessage(0);
						}
//						mHandler.sendEmptyMessage(0);
					}
					
				}
			}

			public void onError(Exception e, QjResult<Map<String,Object>> result) {
				super.onError(e, result);
				mHandler.sendEmptyMessage(Constants.Result_Status.RESULT_FAILURE);
			};

			public void onCompleted(Exception e,
					QjResult<Map<String,Object>> result) {
				super.onCompleted(e, result);
			};
		};
		String path = handler.getCheckData(activity, params,
				new TypeToken<QjResult<Map<String,Object>>>() {
				}, callback);
	}

	/**
	 * 获取短信验证
	 * 
	 * @param activity
	 * @param url
	 * @param map
	 */
	public static void getSmscode(Activity activity,
			Map<String, Object> map ,final Handler mHandler) {
		//LoadingDialog.getInstance(activity).show();
		QJNetUICallback callback = new QJNetUICallback<QjResult<Object>>(
				activity) {
			@Override
			public void onSuccess(QjResult<Object> result) {
				mHandler.sendEmptyMessage(Constants.Result_Status.RESULT_SCCESS);
			}

			public void onError(Exception e, QjResult<Object> result) {
				mHandler.sendEmptyMessage(Constants.Result_Status.RESULT_FAILURE);
			};

			public void onCompleted(Exception e, QjResult<Object> result) {
				super.onCompleted(e, result);
			};
		};
		String url = CommonUtils.getSendCodeUrl(activity);
		String path = handler.postDataObject(activity, url, map,
				new TypeToken<QjResult<Map<String, Object>>>() {
				}, callback);
		//LoadingDialog.getInstance(activity).dismiss();
	}
	
	/**
	 * 登录
	 * @param activity
	 * @param map
	 * @param mHandler
	 */
	public static void doLogin(final Activity activity,
			Map<String, Object> map) {
		QJNetUICallback callback = new QJNetUICallback<QjResult<Map<String, Object>>>(
				activity) {
			@Override
			public void onSuccess(QjResult<Map<String, Object>> result) {
//				mHandler.sendEmptyMessage(Constants.Result_Status.RESULT_SCCESS);
				Map<String, Object> map = result.getResults();
				if(map != null && !map.isEmpty()){
					String token = map.get("token")+"";
					Object object = map.get("is_new_user");
					if(object != null){
					    double is_new_user = Double.parseDouble(object+""); 
						if(is_new_user == 2){
							StatService.onEvent(activity, "register", "eventLabel", 1);
						}
					}
					CommonUtils.setToken(activity, token);
					activity.finish();
				}
			}

			public void onError(Exception e, QjResult<Map<String, Object>> result) {
				if(e == null && result != null){
					ToastUtil.showToast(activity, result.getErrorMsg());
				}
			};

			public void onCompleted(Exception e, QjResult<Map<String, Object>> result) {
				super.onCompleted(e, result);
			};
		};
		String url = CommonUtils.getLoginUrl(activity);
		String path = handler.postDataObject(activity, url, map,
				new TypeToken<QjResult<Map<String, Object>>>() {
				}, callback);
		LoadingDialog.getInstance(activity).dismiss();
	
	}
	/**
	 * 登录
	 * @param activity
	 * @param map
	 * @param mHandler
	 */
	public static void doLoginByPwd(final Activity activity,
			Map<String, Object> map) {
		QJNetUICallback callback = new QJNetUICallback<QjResult<Map<String, Object>>>(
				activity) {
			@Override
			public void onSuccess(QjResult<Map<String, Object>> result) {
//				mHandler.sendEmptyMessage(Constants.Result_Status.RESULT_SCCESS);
				Map<String, Object> map = result.getResults();
				if(map != null && !map.isEmpty()){
					String token = map.get("token")+"";
					/*Object object = map.get("is_new_user");
					if(object != null){
					    double is_new_user = Double.parseDouble(object+""); 
						if(is_new_user == 2){
							StatService.onEvent(activity, "register", "eventLabel", 1);
						}
					}*/
					CommonUtils.setToken(activity, token);
					activity.finish();
				}
			}

			public void onError(Exception e, QjResult<Map<String, Object>> result) {
				if(e == null && result != null){
					ToastUtil.showToast(activity, result.getErrorMsg());
				}
			};

			public void onCompleted(Exception e, QjResult<Map<String, Object>> result) {
				super.onCompleted(e, result);
				LoadingDialog.getInstance(activity).dismiss();
			};
		};
		String url = CommonUtils.getLoginByPwdUrl(activity);
		String path = handler.postDataObject(activity, url, map,
				new TypeToken<QjResult<Map<String, Object>>>() {
				}, callback);
	
	}
	
	/**
	 * 注册
	 * @param activity
	 * @param map
	 * @param mHandler
	 */
	public static void doRegister(final Activity activity,
			Map<String, Object> map) {
		QJNetUICallback callback = new QJNetUICallback<QjResult<Map<String, Object>>>(
				activity) {
			@Override
			public void onSuccess(QjResult<Map<String, Object>> result) {
//				mHandler.sendEmptyMessage(Constants.Result_Status.RESULT_SCCESS);
				Map<String, Object> map = result.getResults();
				if(map != null && !map.isEmpty()){
					String token = map.get("token")+"";
							StatService.onEvent(activity, "register", "eventLabel", 1);
					CommonUtils.setToken(activity, token);
					
				}
				
				activity.finish();
			}

			public void onError(Exception e, QjResult<Map<String, Object>> result) {
				if(e == null && result != null){
					ToastUtil.showToast(activity, result.getErrorMsg());
				}
			};

			public void onCompleted(Exception e, QjResult<Map<String, Object>> result) {
				super.onCompleted(e, result);
				LoadingDialog.getInstance(activity).dismiss();
			};
		};
		String url = CommonUtils.getRegisterUrl(activity);
		String path = handler.postDataObject(activity, url, map,
				new TypeToken<QjResult<Map<String, Object>>>() {
				}, callback);
	
	}
	
	
	
	/**
	 * 重置密码
	 * @param activity
	 * @param map
	 * @param mHandler
	 */
	public static void doResetPsw(final Activity activity,
			Map<String, Object> map) {
		QJNetUICallback callback = new QJNetUICallback<QjResult<Map<String, Object>>>(
				activity) {
			@Override
			public void onSuccess(QjResult<Map<String, Object>> result) {
//				mHandler.sendEmptyMessage(Constants.Result_Status.RESULT_SCCESS);
				Map<String, Object> map = result.getResults();
				if(map != null && !map.isEmpty()){
					String token = map.get("token")+"";
					//StatService.onEvent(activity, "register", "eventLabel", 1);
					CommonUtils.setToken(activity, token);
					
				}
				
				activity.finish();
			}

			public void onError(Exception e, QjResult<Map<String, Object>> result) {
				if(e == null && result != null){
					ToastUtil.showToast(activity, result.getErrorMsg());
				}
			};

			public void onCompleted(Exception e, QjResult<Map<String, Object>> result) {
				super.onCompleted(e, result);
				LoadingDialog.getInstance(activity).dismiss();
			};
		};
		String url = CommonUtils.getResetPswUrl(activity);
		String path = handler.postDataObject(activity, url, map,
				new TypeToken<QjResult<Map<String, Object>>>() {
				}, callback);
	
	}
	
	
	/**
	 * 同步本地数据
	 * @param activity
	 * @param dataVersion
	 * @param mHandler
	 */
	public static void getLocaldataFromNet(final Context activity,
			final int dataVersion , final Handler mHandler) {
		QJNetUICallback callback = new QJNetUICallback<QjResult<Map<String, Map<String,Object>>>>(
				activity) {
			@Override
			public void onSuccess(QjResult<Map<String, Map<String,Object>>> result) {
				if(result != null){
					Map<String, Map<String, Object>> map = result.getResults();
					if(map != null){
						Map<String, Object> localMap = map.get("url");
						if(localMap != null){
							CommonUtils.setDataVersion(activity, "" + dataVersion);
							CommonUtils.setSendCodeUrl(activity,
									localMap.get("sendCodeUrl")+"");
							CommonUtils
									.setLoginUrl(activity, localMap.get("loginUrl")+"");
							CommonUtils.setBookingListUrl(activity,
									localMap.get("bookingListUrl")+"");
							CommonUtils.setDoctoUrl(activity, localMap.get("doctorUrl")+"");
							CommonUtils.setBookingUrl(activity, localMap.get("bookingUrl")+"");
							CommonUtils.setBookingfileUrl(activity, localMap.get("bookingfileUrl")+"");
							CommonUtils.setPaymentUrl(activity, localMap.get("paymentUrl")+"");
							
							CommonUtils.setHospitalUrl(activity, localMap.get("hospitalUrl")+"");
							
							mHandler.sendEmptyMessage(Constants.Result_Status.RESULT_SCCESS);
						}
					}
				}
			}

			public void onError(Exception e, QjResult<Map<String, Map<String,Object>>> result) {
				super.onError(e, result);
				mHandler.sendEmptyMessage(Constants.Result_Status.RESULT_FAILURE);
			};
			
			@Override
			public void onHttpError(int msg) {
				// TODO Auto-generated method stub
				super.onHttpError(msg);
				mHandler.sendEmptyMessage(Constants.Result_Status.RESULT_FAILURE);
			}

			public void onCompleted(Exception e,
					QjResult<Map<String, Map<String,Object>>> result) {
				super.onCompleted(e, result);
			};
		};
		String url = CommonUtils.getLocaldataFromNetUrl(activity);
		String path = handler.getLocalData(activity, url, "",
				new TypeToken<QjResult<Map<String, Map<String,Object>>>>() {
				}, callback);
	}
	
	
	/**
	 * 获取订单列表
	 * @param activity
	 * @param map
	 * @param mHandler
	 */
	public static void getBookingList(final Activity activity,
			String params ,final IDataLoadListenter listenter){
		QJNetUICallback callback = new QJNetUICallback<QjResult<List<OrderItemEntity>>>(activity) {
            @Override
            public void onSuccess(QjResult<List<OrderItemEntity>> result) {
            	super.onSuccess(result);
            	if(result != null){
            		listenter.onSuccess(result.getResults());
            	}
            }
            
            @Override
            public void onError(Exception e, QjResult<List<OrderItemEntity>> result) {
            	super.onError(e, result);
            	listenter.onError(e, result);
            }
            
            @Override
            public void onFailure(QjResult<List<OrderItemEntity>> result) {
            	super.onFailure(result);
            	listenter.onFailure(result);
            }
            
            @Override
            public void onHttpError(int msg) {
            	super.onHttpError(msg);
            	listenter.onHttpError(msg);
            }
            
            @Override
            public void onCompleted( Exception e, QjResult<List<OrderItemEntity>> result ){
            		super.onCompleted( e, result );
            		listenter.onCompleted(e, result);
            }
        };
        String url = CommonUtils.getBookingListUrl( activity );
        String tokenParam = CommonUtils.getTokenParam(activity);
        String path = handler.getBookingList(activity, url, params, new TypeToken<QjResult<List<OrderItemEntity>>>() {}, callback, tokenParam);
	}
	
	/**
	 * 获取订单列表
	 * @param activity
	 * @param map
	 * @param mHandler
	 */
	public static void getBookingList(final Activity activity,String type,
			String params ,final IDataLoadListenter listenter){
		QJNetUICallback callback = new QJNetUICallback<QjResult<List<OrderItemEntity>>>(activity) {
            @Override
            public void onSuccess(QjResult<List<OrderItemEntity>> result) {
            	super.onSuccess(result);
            	if(result != null){
            		listenter.onSuccess(result.getResults());
            	}
            }
            
            @Override
            public void onError(Exception e, QjResult<List<OrderItemEntity>> result) {
            	super.onError(e, result);
            	listenter.onError(e, result);
            }
            
            @Override
            public void onFailure(QjResult<List<OrderItemEntity>> result) {
            	super.onFailure(result);
            	listenter.onFailure(result);
            }
            
            @Override
            public void onHttpError(int msg) {
            	super.onHttpError(msg);
            	listenter.onHttpError(msg);
            }
            
            @Override
            public void onCompleted( Exception e, QjResult<List<OrderItemEntity>> result ){
            		super.onCompleted( e, result );
            		listenter.onCompleted(e, result);
            }
        };
        String url = CommonUtils.getBookingListUrl( activity );
        
        String tokenParam = CommonUtils.getTokenParam(activity);
        
        if (type!=null) {
        	tokenParam = tokenParam + "&bk_status="+type;

		}
        String path = handler.getBookingList(activity, url, params, new TypeToken<QjResult<List<OrderItemEntity>>>() {}, callback, tokenParam);
		
	}
	
	/**
	 * 获取订单信息
	 * @param activity
	 * @param url
	 * @param map
	 * @param listenter
	 */
	public static void getBookingDetail(final Activity activity, String url,
			String params ,final IDataLoadListenter listenter){
		QJNetUICallback callback = new QJNetUICallback<QjResult<OrderItemEntity>>(activity) {
			@Override
			public void onSuccess(QjResult<OrderItemEntity> result) {
				if(result != null){
					listenter.onSuccess(result.getResults());
				}
			}

			 @Override
	            public void onError(Exception e, QjResult<OrderItemEntity> result) {
	            	super.onError(e, result);
	            	listenter.onError(e, result);
	            }
	            
	            @Override
	            public void onFailure(QjResult<OrderItemEntity> result) {
	            	super.onFailure(result);
	            	listenter.onFailure(result);
	            }
	            
	            @Override
	            public void onHttpError(int msg) {
	            	super.onHttpError(msg);
	            	listenter.onHttpError(msg);
	            }
	            
	            @Override
	            public void onCompleted( Exception e, QjResult<OrderItemEntity> result ){
	            		super.onCompleted( e, result );
	            		listenter.onCompleted(e, result);
	            }
		};
		String tokenParam = CommonUtils.getTokenParam(activity);
		String path = handler.getBookingDetial(activity, url, params, new TypeToken<QjResult<OrderItemEntity>>() {}, callback, tokenParam);
	}
	
	/**
	 * 根据条件获取医生列表
	 * @param activity
	 * @param url
	 * @param map
	 * @param listenter
	 * @return
	 */
	public <T extends IResult> String getDoctorList(Activity activity,
			String url, String params ,final IDataLoadListenter listenter) {
			QJNetUICallback callback = new QJNetUICallback<QjResult<List<DoctorsEntity>>>(activity) {
			@Override
			public void onSuccess(QjResult<List<DoctorsEntity>> result) {
				if(result != null){
					listenter.onSuccess(result.getResults());
				}
			}

			@Override
			public void onError(Exception e, QjResult<List<DoctorsEntity>> result) {
				super.onError(e, result);
				if(result != null){
					listenter.onError(e, result.getErrorMsg());
				}
			}

			@Override
			public void onCompleted( Exception e, QjResult<List<DoctorsEntity>> result ) {
				super.onCompleted( e, result );
			}
		};
		return handler.getDoctorList(activity, url, params, new TypeToken<QjResult<List<DoctorsEntity>>>() {} , callback);
	}
	
	/**
	 * 
	 * @param activity
	 * @param url
	 * @param map
	 * @param listenter
	 * @return
	 */
	public <T extends IResult> String doFastBooking(final Activity activity,
			String url, Map<String, Object> map ,final IDataLoadListenter listenter){
		QJNetUICallback callback = new QJNetUICallback<QjResult<Map<String, Object>>>(activity) {
			@Override
			public void onSuccess(QjResult<Map<String, Object>> result) {
				super.onSuccess(result);
				if(result != null){
					listenter.onSuccess(result.getResults());
				}else {
					ToastUtil.showToast(activity, "加载失败！",Toast.LENGTH_SHORT);
				}
			}
			
			@Override
			public void onError(Exception e,
					QjResult<Map<String, Object>> result) {
				super.onError(e, result);
				listenter.onError(e, result);
			}
			
			@Override
			public void onFailure(QjResult<Map<String, Object>> result) {
				super.onFailure(result);
				listenter.onFailure(result);
			}
			
			@Override
			public void onHttpError(int msg) {
				super.onHttpError(msg);
				listenter.onHttpError(msg);
			}
			
			@Override
			public void onCompleted(Exception e,
					QjResult<Map<String, Object>> result) {
				super.onCompleted(e, result);
				listenter.onCompleted(e, result);
			}
		};
		return handler.postDataObject(activity, url, map, new TypeToken<QjResult<Map<String, Object>>>() {}, callback);
	}
	
	/**
	 * 搜索
	 * @param activity
	 * @param key
	 * @param map
	 * @param listenter
	 * @return
	 */
	public <T extends IResult> String doSearch(Activity activity,
			final String key, Map<String, Object> map ,final IDataLoadListenterByFlg listenter){
		String actionUrl = Constants.baseURL + "search"+"?name="+key;

		QJNetUICallback callback = new QJNetUICallback<QjResult<SearchEntity>>(
				activity) {
			@Override
			public void onSuccess(QjResult<SearchEntity> result,
					String url) {
				super.onSuccess(result, url);
				if(result != null){
					listenter.onSuccess(result.getResults(), url);
				}
			}

			public void onError(Exception e, QjResult<SearchEntity> result ,String url) {
				super.onError(e, result);
				if(result != null){
					listenter.onError(e, result.getErrorMsg(),url);
				}
			};

			public void onCompleted(Exception e, QjResult<SearchEntity> result,String url) {
				super.onCompleted(e, result);
			};
		};
		return handler.getDataObject(activity, actionUrl,
				null, new TypeToken<QjResult<SearchEntity>>() {
				}, callback,key);
	}
	
}

